package com.hb.trans.mvp.presenter

import com.basiclib.base.BasePresenter
import com.basiclib.lifecycle.RxLifecycleUtils
import com.basiclib.network.RetryWithDelay
import com.basiclib.network.RxErrorHandler
import com.hb.trans.bean.Base
import com.hb.trans.bean.New
import com.hb.trans.global.ErrorHandleSubscriber
import com.hb.trans.mvp.contract.NewListContract
import com.hb.trans.mvp.model.NewListModel
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import javax.inject.Inject

class NewListPresenter @Inject constructor(model: NewListModel, var errorHandler: RxErrorHandler) :
    BasePresenter<NewListContract.View, NewListContract.Model>(model) {

    fun getNewList(labelId: String, isRefresh: Boolean) {
        mModel.getNewList(labelId)
            .subscribeOn(Schedulers.io())
            .retryWhen(RetryWithDelay())
            .observeOn(AndroidSchedulers.mainThread())
            .compose(RxLifecycleUtils.bindToLifecycle(mRootView!!))
            .subscribe(object : ErrorHandleSubscriber<Base<MutableList<New>>>(errorHandler) {
                override fun next(r: Base<MutableList<New>>) {
                    if (r.code == 1) {
                        mRootView?.setupData(r.data)
                    } else {
                        mRootView?.showShortToast(r.info ?: "")
                    }
                }
            })
    }
}